﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using System.Collections.Generic;
using HRProject.Code.Common;

namespace HRProject.Code.DAO
{
    public class BasicSalaryDAO : BaseDAO
    {
        private static string FEATURE = "workersalary";
        private static string MODE = "basicsalary";

        public BasicSalaryDAO()
            : base ()
        {
        }

        public BLMonthlyBasicSalaryTable Collect(BLJobTitle jobTitleFilter, Guid agentId, string fullName, DateTime month, int totalPage, int pageSize, int pageIndex)
        {
            int startIndex = (pageIndex - 1) * pageSize + 1;
            if (pageIndex > totalPage)
            {
                startIndex = (totalPage - 1) * pageSize + 1;
            }
            else if (pageIndex < 1)
            {
                startIndex = 1;
            }
            int endIndex = startIndex + pageSize - 1;

            BLMonthlyBasicSalaryTable result = new BLMonthlyBasicSalaryTable(month.Year, month.Month);
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@jobTitleId", jobTitleFilter.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@agentId", agentId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@agentFullName", fullName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@date", month, ParameterDirection.Input));
            condition.Add(new BaseCondition("@startIndex", startIndex, ParameterDirection.Input));
            condition.Add(new BaseCondition("@endIndex", endIndex, ParameterDirection.Input));
            DataTable data = select("usp_CollectBasicSalaryInfo", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                BLAgentMonthlyBasicSalary info = new BLAgentMonthlyBasicSalary(result);
                info.Order = (int)data.Rows[i]["rownum"];
                info.Agent.Id = (Guid)data.Rows[i]["AgentId"];
                info.Agent.FullName = (string)data.Rows[i]["FullName"];
                info.SalaryLevel.InfoNumberValue = (int)data.Rows[i]["SalaryLevel"];
                info.Senority.InfoNumberValue = (int)data.Rows[i]["Senority"];
                info.SalaryLevelSalaryCalc = (int)data.Rows[i]["SalaryLevelSalaryCalc"];
                info.SenoritySalaryCalc = (int)data.Rows[i]["SenoritySalaryCalc"];
                info.TotalBasicSalary = (int)data.Rows[i]["TotalBasicSalary"];
                result.AgentBasicSalaryInfos.Add(info);
            }
            return result;
        }

        public int Update(BLAgentMonthlyBasicSalary info, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            DateTime date = new DateTime(info.MonthRef.Year, info.MonthRef.Month, 1);
            condition.Add(new BaseCondition("@agentId", info.Agent.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@date", date, ParameterDirection.Input));
            condition.Add(new BaseCondition("@infoName", info.SalaryLevel.InfoName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@infoValue", info.SalaryLevel.InfoNumberValue, ParameterDirection.Input));
            condition.Add(new BaseCondition("@description", info.SalaryLevel.Description, ParameterDirection.Input));
            condition.Add(new BaseCondition("@feature", FEATURE, ParameterDirection.Input));
            condition.Add(new BaseCondition("@mode", MODE, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", info.SalaryLevel.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_UpdateAgentSalaryInfo", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;

            if (errorCode != 0)
            {
                return result;
            }

            condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@agentId", info.Agent.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@date", date, ParameterDirection.Input));
            condition.Add(new BaseCondition("@infoName", info.Senority.InfoName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@infoValue", info.Senority.InfoNumberValue, ParameterDirection.Input));
            condition.Add(new BaseCondition("@description", info.Senority.Description, ParameterDirection.Input));
            condition.Add(new BaseCondition("@feature", FEATURE, ParameterDirection.Input));
            condition.Add(new BaseCondition("@mode", MODE, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", info.Senority.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            result += update("usp_UpdateAgentSalaryInfo", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;

            if (errorCode == 0)
            {
                BLJobTitle emptyJobTitle = new BLJobTitle();
                BLMonthlyBasicSalaryTable data = Collect(emptyJobTitle, info.Agent.Id, string.Empty, date, 1, 1, 1);
                if (data.AgentBasicSalaryInfos.Count > 0)
                {
                    info.SalaryLevelSalaryCalc = data.AgentBasicSalaryInfos[0].SalaryLevelSalaryCalc;
                    info.SenoritySalaryCalc = data.AgentBasicSalaryInfos[0].SenoritySalaryCalc;
                    info.TotalBasicSalary = data.AgentBasicSalaryInfos[0].TotalBasicSalary;
                }
            }

            return result;
        }
    }
}
